﻿#ifdef __linux__

#include <utility>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h> 

using mmap_t = int;
using fileProperties = struct stat;
using FILE_t = int;

#elif defined(_WIN32)

#include <Windows.h>

using mmap_t = HANDLE;
using fileProperties = WIN32_FILE_ATTRIBUTE_DATA;
using FILE_t = HANDLE;

#endif 

#include <string>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <errno.h>
#include <cstring>
#include <algorithm>

#undef max

#undef min

// 包装mmap，使其可以达到C++STL中string的便携

using ll = long long;

#define END 1000000007

#define PAGEVALUE 4096

class Mmap
{
public:

    Mmap(const std::string path);

    ll size();

    ll capacity();

    bool write(const std::string& writeContent);

    std::string read(size_t start = 0, size_t end = END);

    bool clear();   //清空文件内容， 容量不变

    ~Mmap(); //容量适应内容


private:

    bool expansion(size_t size); //扩容到能包括内容的标准块大小

private:

    FILE_t _fd;    //文件标识符

    size_t _size;   //文件内容大小（字节）

    size_t _capacity;   //文件容量

    fileProperties _attribute{0}; // 文件属性

    char* _start; //映射起始位置

#ifdef _WIN32

    HANDLE _mmap;

#endif
 
};
